//
//  RecordsViewController.m
//  AntsExpress
//
//  Created by slccper on 16/3/9.
//  Copyright © 2016年 slccper. All rights reserved.
//

#import "RecordsViewController.h"
#import "RecordsViewCell.h"
#import "PullTableView.h"
@interface RecordsViewController ()<UITableViewDataSource,UITableViewDelegate,PullTableViewDelegate>
{
    NSMutableArray *_tableList;
    NSUInteger page;
}
@property (weak, nonatomic) IBOutlet UILabel *MemID;
@property (weak, nonatomic) IBOutlet UILabel *Money;
@property (weak, nonatomic) IBOutlet PullTableView *tableView;

@end

@implementation RecordsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title=@"充值/付款記錄";
    if ([[clsOtherFun getMemberId] integerValue]!=0) {
        self.MemID.text = [NSString stringWithFormat:@"會員編號：%ld",(long)[[clsOtherFun getMemberId] integerValue]+600000];
    }else{
        self.MemID.text = [NSString stringWithFormat:@"會員編號："];
    }
    self.Money.text = [NSString stringWithFormat:@"余額（M幣）：%ld",[[clsOtherFun getVIPMoney] integerValue]];
    self.tableView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    self.tableView.pullDelegate = self;
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.backgroundColor = [UIColor lightGrayColor];
    self.tableView.pullBackgroundColor = [UIColor lightGrayColor];
    self.tableView.pullArrowImage = [UIImage imageNamed:@"blackArrow"];
    self.tableView.rowHeight = 80;
    
    // Do any additional setup after loading the view from its nib.
}
-(void)getWebData
{
    page = 1;
    [self loadData];
}
-(void)loadData
{
    if ([clsOtherFun CheckNetworkStatus:YES]) {
        if (!ants) {
            ants = [[clsAntsService alloc]init];
            ants.delegate=self;
        }
        [ants GetConSumpitionLog:[clsOtherFun getencodeName] :page];
    }else{
        [self performSelector:@selector(loadDataSourse) withObject:nil afterDelay:0.1];
    }
    

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)getAntsDataSuccess:(NSInteger)type :(NSMutableDictionary *)pDictionary
{
    NSDictionary *data = [pDictionary objectForKey:@"data"];
    NSString *status = [data objectForKey:@"status"];
    if (type==Ants_GetConSumpitionLog) {
        if ([status integerValue]==200) {
            if ([data objectForKey:@"data"]) {
                if ([[data objectForKey:@"data"]isKindOfClass:[NSArray class]]) {
                    NSArray *list = [data objectForKey:@"data"];
                    if (page>1) {
                        
                        [_tableList addObjectsFromArray:list];
                    }else{
                        _tableList = [NSMutableArray arrayWithArray:list];
                    }
                    
                }
            }else if ([status integerValue]==500)
            {
                [clsOtherFun ShowMsg:[data objectForKey:@"message"]];
            }
        }
        [ants GetUserMoney:[clsOtherFun getencodeName]];
        [self loadDataSourse];
    }else if (type==Ants_GetUserMoney){
        NSDictionary *dic = [pDictionary objectForKey:@"data"];
        NSString *status = [dic objectForKey:@"status"];
        if ([status integerValue]==200) {
            NSString *data = [dic objectForKey:@"data"];
            [clsOtherFun setVIPMoney:[NSString stringWithFormat:@"%.2f",[data floatValue]]];
            self.Money.text = [NSString stringWithFormat:@"M幣：%.2f",[[clsOtherFun getVIPMoney]floatValue]];
        }
    }
    
}
- (void)loadDataSourse
{
    self.tableView.pullTableIsRefreshing=NO;
    self.tableView.pullTableIsLoadingMore=NO;
    self.tableView.pullLastRefreshDate = [NSDate date];
    [self.tableView reloadData];
}
#pragma mark - UITableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _tableList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *CellIdentifier = @"RecordsViewCell";
    
    RecordsViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[NSBundle mainBundle]loadNibNamed:@"RecordsViewCell" owner:self options:nil]lastObject];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        //        cell.parentViewController=self;
    }
    NSDictionary *dic = [_tableList objectAtIndex:indexPath.row];
    [cell setData:dic];
    return cell;
}
-(void)pullTableViewDidTriggerLoadMore:(PullTableView *)pullTableView
{
    page+=1;
    [self loadData];
}
-(void)pullTableViewDidTriggerRefresh:(PullTableView *)pullTableView
{
    page = 1;
    [self loadData];
}
@end
